package org.example.controller.tester;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.TokenStorage;
import org.example.service.impl.MyTaskServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/11
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/tester/task")
public class testerTaskController {
    @Autowired
    MyTaskServiceImpl myTaskService;

    @GetMapping("/list")
    public ApiResponse getTaskList(HttpSession session) {
        try {
            return ApiResponse.ok("获取成功",
                    myTaskService.getTaskList((int)session.getAttribute(TokenStorage.getToken())));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }

    @PostMapping("/accept")
    public ApiResponse acceptTask(String id) {
        try {
            if (myTaskService.acceptTask(Integer.parseInt(id))) return ApiResponse.ok("更新成功");
            else return ApiResponse.failure("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("更新失败");
        }
    }

    @PostMapping("/refuse")
    public ApiResponse refuseTask(String id) {
        System.out.println(id);
        try {
            if (myTaskService.refuseTask(Integer.parseInt(id))) return ApiResponse.ok("更新成功");
            else return ApiResponse.failure("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("更新失败");
        }
    }

    @PostMapping("/finish")
    public ApiResponse finishTask(String id) {
        try {
            if (myTaskService.finishTask(Integer.parseInt(id))) return ApiResponse.ok("更新成功");
            else return ApiResponse.failure("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("更新失败");
        }
    }

    @GetMapping("/map")
    public ApiResponse getIdNameMap(HttpSession session) {
        try {
            return ApiResponse.ok("获取成功",
                    myTaskService.getIdNameMap((int) session.getAttribute(TokenStorage.getToken())));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.failure("获取失败");
        }
    }
}
